var __this;
var DEF_WIDTH_SPACE = 100;
var DEF_HEIGHT_SPACE = 20;
var DEF_LEFT_PADDING = 300;
var DEF_TOP_PADDING = 300;
function TreeView() {
	this.childNodes = new Array();
	this.root = null;
	this.vGroup = G.canvas.vGroup;
	
	this.left = 200;//$(G.canvas.container).width() / 2 - 90;
	this.top = 100;//$(G.canvas.container).height() / 2 - 30;
	this.width = $(G.canvas.container).width();
	this.height = $(G.canvas.container).height()
	
	__this = this;
};
TreeView.prototype.initChild = function() {
	
};
TreeView.prototype.appendChild = function(node) {
	
};
TreeView.prototype.removeChild = function(node) {
	
};
TreeView.prototype.loadJSON = function(url) {
	
};
TreeView.prototype.loadXML = function(url) {
	//var xml = $.get('treeLoader?' + Math.random(), this._handleXML, 'xml');
	var xml = $.ajax({
		type: 'GET',
		url: 'treeLoader?' + Math.random(),
		dataType: 'xml',
		async: true,
		cache: false,
		complete: function(){G.canvas.isInit = true},
		success: this._handleXML
	});
};
TreeView.prototype._handleXML = function(_xml) {
	var root = _xml.documentElement;
	var vGroup = G.canvas.vGroup;
	
	var attrs = root.attributes;
	var attr = {};
	for (var i = 0; i < attrs.length; i++) {
		attr[attrs(i).nodeName] = attrs[i].nodeValue;
	}
	var node = new RootNode(attr);
	__this.root = node;
	__this._loadChild(root, node);
	__this.renderTree();
};
TreeView.prototype._loadChild = function(xmlNode, treeNode) {
	var parentTop = 0;
	var node = null;
	
	var len = xmlNode.childNodes.length;
	for (var i = 0; i < len; i++) {
		var child = xmlNode.childNodes[i];
		
		var attrs = child.attributes;
		var attr = {};
		for (var j = 0; j < attrs.length; j++) {
			attr[attrs(j).nodeName] = attrs[j].nodeValue;
		}
		node = new TreeNode(attr);
		node.deep = treeNode.deep + 1;
		treeNode.appendChild(node);
		this._loadChild(child, node);
	}
};
TreeView.prototype.renderTree = function(node) {
	if (typeof node == 'undefined') {
		node = this.root;
	}
	node.getChildHeight(0);
	
	var t;
	if (node.childNodes.length > 0) {
		t = node.getChildHeight() - (node.lastChild.getChildHeight() + node.lastChild.height - node.firstChild.getChildHeight())/2;
		t += node.height / 2;
	} else t = node.getChildHeight();
	
	var l;
	if (node.deep == 0) l =  DEF_LEFT_PADDING
	else l = DEF_LEFT_PADDING + this.root.width / 2 + (node.width + DEF_WIDTH_SPACE) * node.deep;
	node.render(this.vGroup, l, t);
	
	for (var i = 0; i < node.childNodes.length; i++) {
		this.renderTree(node.childNodes[i]);
		
		var lv = new LineView();
		lv.render(this.vGroup, node.getEast(), node.childNodes[i].getWest());
	}
};

/*TreeNode*/
function TreeNode(attr) {
	this.base = RoundRect;
	
	this.childNodes = new Array();
	this.parentNode = null;
	this.expanded = false;
	this.datas = {};
	this._apply(attr);
	this.clientHeight = 0;
	this.isComputeHeight = false;
	this.deep = 0;
	
	this.previousSibling = null;
	this.nextSibling = null;
	this.firstChild = null;
	this.lastChild = null;
	this.parentNode = null;
	
	if (!this.width) this.width = 100;
	if (!this.height) this.height = 20;
	
	this.base();
	this.createUI();
	this.addEvents();
};
TreeNode.prototype = new RoundRect();
TreeNode.prototype.toString = function() {
	return '[Component RoundRect TreeNode]';
};
TreeNode.prototype.appendChild = function(node) {
	this.childNodes.push(node);
	var index = this.childNodes.length;
	if (index == 1) {
		this.firstChild = node;
	} 
	
	node.parentNode = this;
	var ps = this.childNodes[index-1];
	if (ps) {
		node.previousSibling = ps;
		ps.nextSibling = node;
	} else {
		node.previousSibling = null;
	}
	node.nextSibling = null;
	this.lastChild = node;
};
TreeNode.prototype.isFirstChild = function() {
	return this.previousSibling ? false : true;
};
TreeNode.prototype.removeChild = function(node) {
	var index = this.childNodes.indexOf(node);
	this.childNodes.slice(index, 1);
};
TreeNode.prototype.indexOfSelf = function() {
	if (!this.parentNode) return 0;
	for (var i = 0; i < this.parentNode.childNodes.length; i++) {
		if (this.parentNode.childNodes[i] == this) return i;
	}
	return -1;
};
TreeNode.prototype.indexOfChild = function(o) {
	for(var i = 0, len = this.childNodes.length; i < len; i++){
        if(this.childNodes[i] == o) return i;
    }
    return -1;
};
/**
 * 
 */
TreeNode.prototype.getChildHeight = function(prev) {
	if (this.isComputeHeight) return this.clientHeight;
	
	var h = 0;
	//如果有子节点，本节点的高由自子节点计算二来
	if (this.childNodes.length > 0) {
		for (var i = 0; i < this.childNodes.length; i++) {
			var child = this.childNodes[i];
			//如果是第一个节点，把父节点传进来的值作为参数传入进行子节点递归
			//如果非第一个节点，则把相邻的前一个节点的高作为参数传入
			if (i > 0){
				h = child.getChildHeight(this.childNodes[i-1].getChildHeight());
			} else {
				h = child.getChildHeight(prev);
			}
		}
	} else {
		//以下是如果没有子节点的情况
		if (this.isFirstChild) {	//如果本节点属于父节点的第一个子节点则高等于自己
			h = prev + DEF_HEIGHT_SPACE + this.height;
		} else {	//如果不是第一个子节点，则高等于自己加上前一个兄弟子节点的高
			h = this.previousSibling.getChildHeight() + DEF_HEIGHT_SPACE + this.height;
		}
	}
	
	this.isComputeHeight = true;
	this.clientHeight = h;
	return h;
};
TreeNode.prototype._getAllChildsHeight = function() {
	
};
TreeNode.prototype.expand = function() {
	
};
TreeNode.prototype.collapse = function() {
	
};
TreeNode.prototype._apply = function(attr) {
	if (typeof attr == 'string') {
		this.text = attr;
	}
	if (typeof attr == 'object') {
		for (var p in attr) {
			this[p] = attr[p];
			this.datas[p] = attr[p];
		}
	}
	if (!this.id) this.id = 'TreeNode' + G.canvas.getID();
	if (!this.text) this.text = this.id;
};
TreeNode.prototype.createUI = function() {
	this.getUI().style.width = this.width;
	this.getUI().style.height = this.height;
	this.style['font-size'] = '13px';
	this.style['font-weight'] = 'normal';
	this._createUI();
};
TreeNode.prototype.addEvents = function() {
	var _node = this;
	this.getUI().onclick = function(e) {
		var s = '<table>';
		for (var i in _node.datas)
			s += '<tr><td class="tip-name">'+i+'：</td><td class="tip-value">' + _node.datas[i] + '<td></tr>';
		s += '</table>'
		Tip(s, BALLOON,true, FADEIN,400, FADEOUT,400, ABOVE,true, WIDTH,260, PADDING,8, TEXTALIGN,'justify', OFFSETX,-20, DELAY,0)
	};
	
	this.getUI().onmouseover = function(e) {
		G.canvas.getFocusDiv().show(_node);
	};
	
	this.getUI().onmouseout = function(e) {
		G.canvas.getFocusDiv().hide(_node);
	};
};

/*RootNode*/
function RootNode(attr) {
	this.base = TreeNode;
	this.width = 200;
	this.height = 40;
	this.deep = 0;
	this.firstChild = this;
	this.lastChild = this;
	this.base(attr);
};
RootNode.prototype = new TreeNode();
RootNode.prototype.createUI = function() {
	this.getUI().style.width = this.width;
	this.getUI().style.height = this.height;
	this.style['font-size'] = '20px';
	this.style['font-weight'] = 'bold';
	this._createUI();
};